const { query, getCount } = require('../../utils/dbTools');
const tableName = 'log';
const adminTableName = 'admin';
/**
 * 获取数据总条数
 * 
*/
const logTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const result = await getCount(tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取日志列表
 * 
*/
const logList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    if (!page || !size) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    let sql = `select l.id,l.type,l.remark,l.ip,l.url,l.create_time, a.username as account,a.phone from ${tableName} l`;
    sql += ` inner join ${adminTableName} a on l.accountid=a.id order by l.id desc limit ${(page - 1) * size},${size}`;
    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}


module.exports = {
    logList,
    logTotal
}